package com.example.a4;

import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.view.*;
import android.view.View.*;
import android.content.*;


public class MainActivity extends AppCompatActivity {
    RadioButton r1=null;
    RadioButton r2=null;
    RadioButton r3=null;
    RadioButton r4=null;
    RadioGroup rg=null;
    RadioButton curRB=null;
    Button btn1_ok=null;
    Button btn2_cancel=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1_ok=(Button) findViewById(R.id.ok);
        btn2_cancel=(Button) findViewById(R.id.cancel);
        btn1_ok.setEnabled(false);
        rg=(RadioGroup) findViewById(R.id.radioGroup);
        r1=(RadioButton) findViewById(R.id.a);
        r2=(RadioButton) findViewById(R.id.b);
        r3=(RadioButton) findViewById(R.id.c);
        r4=(RadioButton) findViewById(R.id.d);
        rg.setOnCheckedChangeListener(new mChangeRadio());
        btn1_ok.setOnClickListener(new btn_ok());
        btn2_cancel.setOnClickListener(new btn_cancel());
    }
    class btn_ok implements OnClickListener{
        public void onClick(View v) {
            Intent intent=new Intent();
            intent.setClass(MainActivity.this,otherActivity.class);
            Bundle bundle=new Bundle();
            bundle.putString("ans",curRB.getText().toString());
            intent.putExtras(bundle);
            //启动方式1：无requestCode
            //MainActivity.this.startActivity(intent);
            //启动方式2：含requestCode，返回时对应调用onActivityResult
            startActivityForResult(intent,0);
        }
    }
    class btn_cancel implements OnClickListener {
        public void onClick(View v){
            rg.clearCheck();
            btn1_ok.setEnabled(false);
            setTitle("");
        }
    }
    class mChangeRadio implements RadioGroup.OnCheckedChangeListener{
        public void onCheckedChanged(RadioGroup group,int checkedId){
            curRB=(RadioButton) findViewById(checkedId);
            btn1_ok.setEnabled(true);
        }
    }
    protected void onActivityResult(int requestCode,int resultCode/*从子Activity返回的值*/,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        //包含requestCode的判断（用于区分不同的请求所返回对应的结果）
        if(requestCode==0&&resultCode==RESULT_OK){
            Bundle bundle=data.getExtras();
            String a=String.valueOf(resultCode);
            setTitle(a);
        }
        /*
        //不包含requestCode的判断，根据子Activity返回的resultCode作相应处理
        switch (resultCode){
            case RESULT_OK:
                Bundle bundle=data.getExtras();
                String ans=bundle.getString("ans");
                break;
            default:break;
        }
        */
    }
}
